var express = require("express");
var router = express.Router();
var sqlFn = require("../mysql");
var config = require("../config/config.js");
var error = require("../data/error.json");
var success = require("../data/success.json");

// 添加具体路径，解决跨域问题
router.all(config.urlObj.all, function(req, res, next) {
	// 跨域处理
	res.header("Access-Control-Allow-Origin", "*");
	res.header("Access-Control-Allow-Headers", "X-Requested-With");
	res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
	res.header("X-Powered-By", ' 3.2.1');
	res.header("Content-Type", "application/json;charset=utf-8");
	next();
})

// 查询全部数据
router.get(config.urlObj.all,function(req,res){
  var sql = "select * from userinfolist";
  sqlFn(sql,null,function(data){
    res.send(data)
  })
})


// router.all(config.urlObj.login, function(req, res, next) {
// 	// 跨域处理
// 	res.header("Access-Control-Allow-Origin", "*");
// 	res.header("Access-Control-Allow-Headers", "X-Requested-With");
// 	res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
// 	res.header("X-Powered-By", ' 3.2.1');
// 	res.header("Content-Type", "application/json;charset=utf-8");
// 	next();
// })

router.post(config.urlObj.login,function(req,res){
	var sql = "SELECT * FROM userinfolist WHERE `username`=? AND `password`=?";
	var username = req.param("username");
    var password = req.param("password");
	var arr = [username,password]
	sqlFn(sql,arr,function(data){
		res.send(data)
	})
})

// router.all(config.urlObj.register, function(req, res, next) {
// 	// 跨域处理
// 	res.header("Access-Control-Allow-Origin", "*");
// 	res.header("Access-Control-Allow-Headers", "X-Requested-With");
// 	res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
// 	res.header("X-Powered-By", ' 3.2.1');
// 	res.header("Content-Type", "application/json;charset=utf-8");
// 	next();
// })

//添加数据
router.post(config.urlObj.register,function(req,res){
  //读取参数
  var username = req.param("username");
  var password = req.param("password");
   // console.log("1");
  // var repassword=req.param("repassword")
  var sql = "insert into userinfolist values (?,?)";
  // var sql = `select * from usersheet where username="${username}" and password="${password}"`;

  var arr = [username,password]
  sqlFn(sql,arr,function(data){
    if(data.affectedRows){
      res.send(success)
    }else{
      res.send(error8)
    }
  })
})
router.all(config.urlObj.use, function(req, res, next) {
	// 跨域处理
	res.header("Access-Control-Allow-Origin", "*");
	res.header("Access-Control-Allow-Headers", "X-Requested-With");
	res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
	res.header("X-Powered-By", ' 3.2.1');
	res.header("Content-Type", "application/json;charset=utf-8");
	next();
})

// 查询username数据
router.post(config.urlObj.use,function(req,res){
	
	var username = req.param("username");
    // var password = req.param("password");
	var arr = [username]
	var sql = "SELECT * FROM userinfolist WHERE `username`=? ";
	sqlFn(sql,arr,function(data){
		res.send(data)
	})
})


module.exports = router;
